package com.prop.utils;

import com.prop.FilePropertyApi;
import org.dom4j.DocumentException;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class FilePropertyUtils {

    /**
     * 拼接需要添加到文本框内的字符串
     *
     * @param classificationLevel  密级
     * @param classificationPeriod 保密期限
     * @return str
     */
    public static String getClassificationContext(String classificationLevel, String classificationPeriod) {

        String secret = "";
        String year = "";
        if (classificationPeriod != "") {
            String[] secretParts = classificationPeriod.split(",");
            year = secretParts[0];
        }
        switch (classificationLevel) {
            case "0":
                secret = "非密 ";
                break;
            case "64":
                secret = "内部 ";
                break;
            case "88":
                secret = "普通商密▲";
                if (year != "") {
                    year = year == "99" ? "长期" : year + "年";
                } else {
                    year = "10年";
                }
                break;
            case "108":
                secret = "核心商密▲";
                if (year != "") {
                    year = year == "99" ? "长期" : year + "年";
                } else {
                    year = "10年";
                }
                break;
            case "128":
                secret = "秘密★";
                if (year != "") {
                    year = year == "99" ? "长期" : year + "年";
                } else {
                    year = "10年";
                }
                break;
            case "192":
                secret = "机密★";
                if (!"".equals(year)) {
                    year = year == "99" ? "长期" : year + "年";
                } else {
                    year = "20年";
                }
                break;
        }

        return secret+year;
    }


    public static String getFileProperty(String filePath) throws IOException {
        // 保存原始的 System.out
        PrintStream originalOut = System.out;

        // 将 System.out 重定向到一个空输出流
        System.setOut(new PrintStream(new OutputStream() {
            public void write(int b) {
                // 什么也不做，即忽略所有输出
            }
        }));
        // 调用 DLL 相关的代码
        String res = FilePropertyApi.getFileProperty(filePath);
        // 恢复原始的 System.out
        System.setOut(originalOut);

        return res;
    }

    public static String setFileProperty(String filePath, String jsonstr) throws DocumentException {
        // 保存原始的 System.out
        PrintStream originalOut = System.out;

        // 将 System.out 重定向到一个空输出流
        System.setOut(new PrintStream(new OutputStream() {
            public void write(int b) {
                // 什么也不做，即忽略所有输出
            }
        }));
        // 调用 DLL 相关的代码
        String res = FilePropertyApi.setFileProperty(filePath, jsonstr);
        // 恢复原始的 System.out
        System.setOut(originalOut);

        return res;
    }


}
